package com.zekdot.registerclient.action;

import com.alibaba.fastjson.JSONArray;
import com.zekdot.registerclient.entity.EnrollDataEntity;
import com.zekdot.registerclient.entity.EnrollMetaEntity;
import com.zekdot.registerclient.entity.EnrollTempletEntity;
import com.zekdot.registerclient.service.EnrollDataService;
import com.zekdot.registerclient.service.EnrollMetaService;
import com.zekdot.registerclient.service.EnrollTempletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * Create by zekdot on 20-4-4.
 */
@Controller
@RequestMapping("/enroll")
public class EnrollAction {

    @Autowired
    private EnrollMetaService enrollMetaService;
    @Autowired
    private EnrollDataService enrollDataService;
    @Autowired
    private EnrollTempletService enrollTempletService;
    @RequestMapping(value = "/newEnroll",method = RequestMethod.POST)
    @ResponseBody
    public String newEnroll(EnrollMetaEntity enrollMetaEntity,long planEndTimeLong,String userId){
//        System.out.println(planEndTimeLong);
        enrollMetaEntity.setPlanEndTime(new Timestamp(planEndTimeLong));
        return enrollMetaService.newEnroll(enrollMetaEntity,userId).toString();
    }

    @RequestMapping(value = "/preEnrollUsers",method = RequestMethod.POST)
    @ResponseBody
    public String addPreEnrollUsers(String usernamesStr,String eId,String userId){
        List<String> usernames = JSONArray.parseArray(usernamesStr,String.class);
        return enrollDataService.addPreEnrollUsers(usernames,eId,userId).toString();
    }

    @RequestMapping("/moreEnrollMeta")
    @ResponseBody
    public String moreEnrollMeta(String userId, long lastTimeLong,int length){
        Date lastTime = new Date(lastTimeLong);
        return enrollMetaService.getMoreEnrollMeta(userId, lastTime, length).toString();
    }

    /**
     * 存储一个模板
     * @param userId 用户id
     * @param enrollTempletEntity 模板实体
     * @return
     */
    @RequestMapping(value = "/saveTemplet",method = RequestMethod.POST)
    @ResponseBody
    public String saveTemplet(String userId,EnrollTempletEntity enrollTempletEntity){
        return enrollTempletService.save(userId,enrollTempletEntity).toString();
    }

    /**
     * 获取模板
     * @param id 模板id
     * @return
     */
    @RequestMapping("/getTemplet")
    @ResponseBody
    public String getTemplet(String userId,String id){
        return enrollTempletService.get(userId,id).toString();
    }

    /**
     * 根据关键字搜索模板列表
     * @param title 模板名称关键字
     * @param lastId 最后一条id
     * @param length 长度
     * @return
     */
    @RequestMapping("/searchTemplets")
    @ResponseBody
    public String searchTemplets(String userId,String title,String lastId,int length){
        return enrollTempletService.search(userId,title,lastId,length).toString();
    }

    /**
     * 根据关键字搜索模板列表
     * @param userId 模板名称关键字
     * @param lastId 最后一条id
     * @param length 长度
     * @return
     */
    @RequestMapping("/selfTemplets")
    @ResponseBody
    public String selfTemplets(String userId,String lastId,int length){
        return enrollTempletService.self(userId,lastId,length).toString();
    }

    /**
     * 删除模板
     * @param userId 用户id
     * @param id 模板id
     * @return
     */
    @RequestMapping(value = "/deleteTemplet",method = RequestMethod.POST)
    @ResponseBody
    public String deleteTemplet(String userId,String id){
        return enrollTempletService.delete(userId,id).toString();
    }

    /**
     * 获取报名元数据
     * @param eId 元数据id
     * @param token 令牌
     * @param userId 用户名
     * @return
     */
    @RequestMapping("/getEnrollMeta")
    @ResponseBody
    public String getEnrollMeta(String eId,String token,String userId){
        return enrollMetaService.getEnrollMeta(eId,token,userId).toString();
    }
    /**
     * 提交报名数据
     * @param userId
     * @param enrollDataEntity
     * @return
     */
    @RequestMapping(value = "/submitEnroll",method = RequestMethod.POST)
    @ResponseBody
    public String submitEnroll(String userId,EnrollDataEntity enrollDataEntity){
        return enrollDataService.enroll(userId,enrollDataEntity).toString();
    }

    /**
     * 获取报名结果
     * @param userId 用户id
     * @param eId 报名id
     * @return
     */
    @RequestMapping("/enrollResult")
    @ResponseBody
    public String enrollResult(String userId,String eId){
        return enrollDataService.getEnrollResult(eId,userId).toString();
    }

    /**
     * 获取自己填写的报名列表
     * @param userId
     * @return
     */
    @RequestMapping("/moreEnrollData")
    @ResponseBody
    public String moreEnrollData(String userId, long lastTimeLong,int length){
        Date lastTime = new Date(lastTimeLong);
        return enrollDataService.moreEnrollData(userId,lastTime,length).toString();
    }

    /**
     * 获取报名元数据的描述
     * @param userId 用户名
     * @param eId 报名id
     * @param token 令牌
     * @return
     */
    @RequestMapping("/metaDesc")
    @ResponseBody
    public String metaDesc(String userId,String eId,String token){
        return enrollMetaService.getMetaDesc(eId,token,userId).toString();
    }

    /**
     * 获取所有报名数据的描述部分
     * @param userId 用户id
     * @param eId 报名id
     * @param lastTimeLong 最后一条的时间getTime
     * @param length 长度
     * @return
     */
    @RequestMapping("/dataDescs")
    @ResponseBody
    public String dataDescs(String userId,String eId,long lastTimeLong,int length){
        Date lastTime = new Date(lastTimeLong);
        return enrollDataService.getDataDescsByEid(userId,eId,lastTime,length).toString();
    }

    /**
     * 获取报名数据的详情
     * @param userId
     * @param eId
     * @param username
     * @return
     */
    @RequestMapping("/enrollDataDetail")
    @ResponseBody
    public String enrollDataDetail(String userId,String eId,String username){
        return enrollDataService.enrollDataDetail(userId,username,eId).toString();
    }

    @RequestMapping(value = "/updateDataStatus",method = RequestMethod.POST)
    @ResponseBody
    public String updateDataStatus(String userId,String eId,String username,int status,String rejectReason){
        return enrollDataService.updateStatus(eId,userId,username,status,rejectReason).toString();
    }

    @RequestMapping(value = "/updateMetaStatus",method = RequestMethod.POST)
    @ResponseBody
    public String updateMetaStatus(String userId,String eId){
        return enrollMetaService.updateEnrollStatus(eId, userId, 1).toString();
    }

    /**
     * 根据报名id和token获取当前未审核的用户列表
     * @param eId 报名id
     * @param operaToken 操作token
     * @param length 列表长度
     * @return
     */
    @RequestMapping(value = "/enrollDataByToken")
    @ResponseBody
    public String getEnrollDataByToken(String eId,String operaToken,int length){
        return enrollDataService.getEnrollDataByToken(eId,operaToken,length).toString();
    }

    /**
     * 审核报名列表中的用户
     * @param eId 报名id
     * @param reqArrayStr 审核JSON字符串
     * @param operaToken 操作token
     * @return
     */
    @RequestMapping(value = "/checkUserByToken",method = RequestMethod.POST)
    @ResponseBody
    public String checkUserByToken(String eId,String reqArrayStr,String operaToken){
        return enrollDataService.checkUserByToken(eId,reqArrayStr,operaToken).toString();
    }

    /**
     * 获取临时操作token
     * @param eId
     * @return
     */
    @RequestMapping("/getTempToken")
    @ResponseBody
    public String getTempToken(String eId){
        return enrollMetaService.getTempToken(eId).toString();
    }

    /**
     * 获取全部通过用户的报名数据
     * @param tempToken
     * @return
     */
    @RequestMapping("/getExcelData")
    @ResponseBody
    public String getExcelData(String tempToken){
        return enrollDataService.getExcelData(tempToken).toString();
    }

    /**
     * 获取重用报名数据
     * @param eId 报名id
     * @return
     */
    @RequestMapping("/getReuseEnrollData")
    @ResponseBody
    public String getReuseEnrollData(String eId){
        return enrollMetaService.getReuseEnrollData(eId).toString();
    }

    /**
     * 获取是否提交过该报名
     * @param eId 报名id
     * @param userId 用户id
     * @return

    @RequestMapping("/enrollDataStatus")
    @ResponseBody
    public String enrollDataStatus(String eId,String userId){
        return enrollDataService.enrollDataStatus(eId,userId).toString();
    }
     */
}
